<template>
  <div class="login-container">
    <!-- 顶部导航 -->
    <van-nav-bar title="登录" class="page-nav-bar"> </van-nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        name="用户名"
        placeholder="请输入手机号"
        v-model="user.mobile"
        maxlength="11"
        type="number"
        :rules="userFormRules.mobile"
      >
        <i slot="left-icon" class="iconfont icon-shouye"></i>
      </van-field>
      <van-field
        name="密码"   
        v-model="user.code"
        maxlength="6"
        placeholder="请输入验证码"
        type="number"
        :rules="userFormRules.code"
      >
        <i slot="left-icon" class="iconfont icon-shouye"></i>
        <template #button>
          <van-button class="send-sms-btn" round size="small" type="default"
            >发送验证码</van-button
          >
        </template>
      </van-field>
      <div class="login-btn-wrap">
        <van-button class="login-btn" block type="info" native-type="submit">
          登录
        </van-button>
      </div>
    </van-form>
  </div>
</template>

<script>
import { login } from "@/api/user.js";
export default {
  name: "LoginPage",
  components: {},
  props: {},
  data() {
    return {
      user: {
        mobile: "",
        code: ""
      },
      userFormRules: {
        mobile: [
          {
            required: true,
            message: "手机号不能为空"
          },
          {
            pattern: /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-7|9])|(?:5[0-3|5-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[1|8|9]))\d{8}$/,
            message: "手机号格式错误"
          }
        ],
        code: [
          {
            required: true,
            message: "验证码不能为空"
          },
          {
            pattern: /^\d{6}$/,
            message: "验证码格式错误"
          }
        ]
      }
    };
  },
  computed: {},
  watch: {},
  created() {},
  mounted() {},
  methods: {
    async onSubmit(values) {
      //加载loading提示
      this.$toast.loading({
        message: "登录中...",
        forbidClick: true,
        loadingType: "spinner", //加载图标类型, 可选值为 spinner
        duration: 0 //展示时长(ms)
      });
      const user = this.user;
      try {
        const res = await login(user);
        this.$toast.success("登录成功");
      } catch (err) {
        if (err.response.status === 400) {
          this.$toast.fail("登录失败");
        }
      }
    }
  }
};
</script>

<style scoped lang="less">
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .send-sms-btn {
    width: 152px;
    height: 46px;
    line-height: 46px;
    background-color: #ededed;
    font-size: 22px;
    color: #666;
  }
  .login-btn-wrap {
    padding: 53px 33px;
    .login-btn {
      background-color: #6db4fb;
      border: none;
    }
  }
}
</style>
